<template>
    <div class="tag-bar-container">
      <div class="tag-bar" ref="tagBar">
        <button
          class="tag"
          :class="{ active: store.selectedTagId === 'all' }"
          @click="store.selectedTagId = 'all'"
        >
          全部 ({{ store.tagCounts.all }})
        </button>
        
        <button
          v-for="tag in store.tags"
          :key="tag.id"
          class="tag"
          :class="{ active: store.selectedTagId === tag.id }"
          @click="store.selectedTagId = tag.id"
          
        >
          <template v-if="editingTag !== tag.id">
            <span @dblclick.stop="startEditing(tag.id)">{{ tag.name }} ({{ store.tagCounts[tag.id] || 0 }})</span>
            <div class="tag-actions">
              <button 
                class="action-btn" 
                @click.stop="deleteTag(tag.id)"
                :disabled="store.tagCounts[tag.id] > 0"
                v-if="store.tagCounts[tag.id] == 0"
              >
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M4 7H20M10 11V17M14 11V17M5 7L6 19C6 20.1 6.9 21 8 21H16C17.1 21 18 20.1 18 19L19 7M9 7V4C9 3.4 9.4 3 10 3H14C14.6 3 15 3.4 15 4V7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </button>
            </div>
          </template>
          <template v-else>
            <input
              type="text"
              v-model="editTagName"
              @keyup.enter="saveEdit(tag.id)"
              @keyup.escape="cancelEdit"
              ref="editInput"
              class="tag-edit-input"
            />
            <div class="tag-actions">
              <button class="action-btn" @click.stop="saveEdit(tag.id)">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M5 13L9 17L19 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </button>
              <button class="action-btn" @click.stop="cancelEdit">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M18 6L6 18M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </button>
            </div>
          </template>
        </button>
        
        <button class="add-tag-btn" @click="showAddTagInput = true" v-if="!showAddTagInput">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M12 4V20M4 12H20" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
          </svg>
          添加标签
        </button>
        
        <div class="add-tag-input" v-if="showAddTagInput">
          <input
            type="text"
            v-model="newTagName"
            @keyup.enter="addNewTag"
            @keyup.escape="cancelAddTag"
            ref="addTagInput"
            placeholder="输入标签名"
          />
          <div class="tag-actions">
            <button class="action-btn" @click="addNewTag">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M5 13L9 17L19 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </button>
            <button class="action-btn" @click="cancelAddTag">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M18 6L6 18M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </button>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, nextTick } from 'vue'
  import { useNoteStore } from '../stores/noteStore'
    import { watch } from 'vue'
    
  const store = useNoteStore()
  
  const editingTag = ref(null)
  const editTagName = ref('')
  const showAddTagInput = ref(false)
  const newTagName = ref('')
  const editInput = ref(null)
  const addTagInput = ref(null)
  
  function startEditing(tagId) {
    const tag = store.tags.find(t => t.id === tagId)
    if (tag) {
      editingTag.value = tagId
      editTagName.value = tag.name
      nextTick(() => {
        editInput.value?.focus()
      })
    }
  }
  
  function saveEdit(tagId) {
    if (editTagName.value.trim()) {
      store.updateTag(tagId, editTagName.value.trim())
    }
    cancelEdit()
  }
  
  function cancelEdit() {
    editingTag.value = null
    editTagName.value = ''
  }
  
  function deleteTag(tagId) {
    if (store.tagCounts[tagId] === 0) {
      store.deleteTag(tagId)
      if (store.selectedTagId === tagId) {
        store.selectedTagId = 'all'
      }
    }
  }
  
  function addNewTag() {
    if (newTagName.value.trim()) {
      const newTag = store.addTag(newTagName.value.trim())
      store.selectedTagId = newTag.id
      // 为新标签添加一个默认便签
      store.addNote(newTag.id)
      cancelAddTag()
    }
  }
  
  function cancelAddTag() {
    showAddTagInput.value = false
    newTagName.value = ''
  }
  
  // 当点击添加标签按钮时，自动聚焦输入框
  watch(showAddTagInput, (val) => {
    if (val) {
      nextTick(() => {
        addTagInput.value?.focus()
      })
    }
  })
  </script>
  
  <style lang="scss" scoped>
  @import '../styles/_variables.scss';
  @import '../styles/_mixins.scss';
  
  .tag-bar-container {
    // position: sticky;
    // top: 60px; // header高度
    background-color: $header-bg;
    z-index: 5;
    box-shadow: $shadow-sm;
    padding: 0.5rem 1rem;
  }
  
  .tag-bar {
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    padding-bottom: 0.5rem;
    scrollbar-width: thin;
    
    &::-webkit-scrollbar {
      height: 4px;
    }
    
    &::-webkit-scrollbar-thumb {
      background-color: rgba($primary-color, 0.3);
      border-radius: 2px;
    }
  }
  
  .tag, .add-tag-btn, .add-tag-input {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border-radius: $border-radius;
    white-space: nowrap;
    font-size: 0.875rem;
    transition: all 0.2s ease;
  }
  
  
  .tag {
    position: relative;
    background-color: $tag-bg;
    color: $text-color;
    border: none;
    cursor: pointer;
    gap: 0rem;
    &:hover {
      background-color: darken($tag-bg, 5%);
    }
    
    &.active {
      background-color: $primary-color;
      color: white;
      
      .tag-actions .action-btn {
        color: white;
      }
    }
  }
  
  .tag-actions {
    display: flex;
    gap: 0.25rem;
    margin-left: 0.5rem;
  }
  
  .action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    background: none;
    border: none;
    color: $text-secondary;
    cursor: pointer;
    border-radius: 50%;
    
    &:hover {
      background-color: rgba(0, 0, 0, 0.1);
    }
    
    &:disabled {
      opacity: 0.5;
      cursor: not-allowed;
    }
    
    svg {
      width: 16px;
      height: 16px;
    }
  }
  
  .tag-edit-input, .add-tag-input input {
    flex: 1;
    min-width: 100px;
    padding: 0.25rem 0.5rem;
    border: 1px solid $border-color;
    border-radius: $border-radius-sm;
    font-size: 0.875rem;
    
    &:focus {
      outline: none;
      border-color: $primary-color;
    }
  }
  
  .add-tag-btn {
    background-color: transparent;
    color: $primary-color;
    border: 1px dashed $primary-color;
    cursor: pointer;
    
    &:hover {
      background-color: rgba($primary-color, 0.1);
    }
    
    svg {
      width: 16px;
      height: 16px;
    }
  }
  
  .add-tag-input {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background-color: white;
    border: 1px solid $border-color;
    border-radius: $border-radius;
    
    input {
      border: none;
      min-width: 120px;
      
      &:focus {
        border: none;
      }
    }
  }
  </style>